﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using Bussiness;
namespace ShoppingCartWEB
{
    public partial class Customer1 : System.Web.UI.Page
    {
        CustomerManager cusMan = new CustomerManager();
        ProductManager proMan = new ProductManager();
        ShoppingCard cart = new ShoppingCard();
        protected void Page_Load(object sender, EventArgs e)
        {
            cart.Cards = GetCart();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                
                Customer cus = new Customer();
                cus.CustomerName = txtName.Text;
                cus.Address = txtAddress.Text;
                cus.Company = txtCompany.Text;
                cus.Email = txtEmail.Text;
                cus.Phone = txtPhone.Text;
                cusMan.AddNewCustomer(cus);

                Order ord = new Order();
                ord.OrderName = "Đơn hàng mua Điện thoại";
                ord.status = false;

                ord.CustomerID = cus.CustomerID;
                ord.OrderDate = DateTime.Now;
                cusMan.AddNewOrder(ord);

                cusMan.AddNewOrderDetail(cart.Cards, ord.OrderID);

                Response.Redirect("Thanks.aspx");
                Session["Cart"] = null;
                
            }
        }

        private Dictionary<Product, int> GetCart()
        {
            if (Session["Cart"] == null)
                Session.Add("Cart", new Dictionary<Product, int>());
            return (Dictionary<Product, int>)Session["Cart"];
        }
    }
}